module.exports = function ($scope,$stateParams, $resource, $state, $http, toaster) {
    var tcode = $stateParams.tcode
    console.log(tcode);
    /**
     * 初始化经度
     */
    $scope.longitude = 123.492522;
    /**
     * 初始化纬度
     */
    $scope.latitude= 41.812736;
/**
         * 初始化百度地图
         */
        
        $scope.initBap = function () {
          $scope.map = new BMap.Map("container"); // 创建地图实例
          $scope.map.centerAndZoom(new BMap.Point($scope.longitude, $scope.latitude), 15);  // 初始化地图，设置中心点坐标和地图级别
          $scope.map.addControl(new BMap.NavigationControl());
          $scope.map.addControl(new BMap.ScaleControl());
          $scope.map.addControl(new BMap.OverviewMapControl());
          $scope.map.enableScrollWheelZoom(true);

          var driving = new BMap.DrivingRoute($scope.map);

          // var p1 = new BMap.Point(123.485482,41.808768);
          // var p2 = new BMap.Point(123.497699,41.81358);
          
          // driving.search(p1, p2);
          driving.setSearchCompleteCallback(function(){
            var pts = driving.getResults().getPlan(0).getRoute(0).getPath();    //通过驾车实例，获得一系列点的数组
    
            var polyline = new BMap.Polyline(pts);     
            $scope.map.addOverlay(polyline);
            // $scope.addmarker(p1);
            // $scope.addmarker(p2);
          })




        $scope.addmarker = function (point,name){
            var myIcon = new BMap.Icon("http://shangke2.oss-cn-qingdao.aliyuncs.com/img/map_mark.png", new BMap.Size(21,60));
            var marker = new BMap.Marker(point,{icon:myIcon});
            $scope.map.addOverlay(marker);
            $scope.createTag(marker,name);
          }
        };

       
        $scope.createTag = function (marker,name){

          //标注
          var text =  name ; 
          //给标记添加点击事件以及显示窗口信息
           var infoWindow = new BMap.InfoWindow( text );
           marker.addEventListener("click", function () { 
             this.openInfoWindow(infoWindow);
             
           });
      }


      $resource('/api/as/ggc/tourgroupafter/findGTList', {}, {}).save({'tourgroup_code':tcode},function (res) {
    
        if (res.errcode !== 0) {
          alert("数据获取失败");
          return;
        }
        var resdata = res.data;
        console.log(res.data.length);
        for(var no in resdata){
          var log = resdata[no].center_log;
          var lat = resdata[no].center_lat;
          var name = resdata[no].center_name;
          var marker = new BMap.Point(log,lat);
          $scope.addmarker(marker,name);
        }
        /*以上是标点    以下是画线*/
        var driving = new BMap.DrivingRoute($scope.map);
        for(i=1;i<res.data.length;i++){
          var p1 = new BMap.Point(res.data[i-1].center_log,res.data[i-1].center_lat);
          var p2 = new BMap.Point(res.data[i].center_log,res.data[i].center_lat);
          
          driving.search(p1, p2);
          driving.setSearchCompleteCallback(function(){
            var pts = driving.getResults().getPlan(0).getRoute(0).getPath();    //通过驾车实例，获得一系列点的数组
    
            var polyline = new BMap.Polyline(pts);     
            $scope.map.addOverlay(polyline);
          })
        }
        $scope.map.panTo(new BMap.Point(resdata[0].center_log, resdata[0].center_lat));
      });

        $scope.initBap();
        
    }